package com.example.flipper;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;

public class ListenerTouchViewFlipper implements OnTouchListener {
	public float init_x;
	private ViewFlipper vf;
	public ListenerTouchViewFlipper(float init_x, ViewFlipper vf)
	{
		this.init_x = init_x;
		this.vf = vf;
	}
	public ListenerTouchViewFlipper() {
		// TODO Auto-generated constructor stub
	}

	@Override
	
	public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: //Cuando el usuario toca la pantalla por primera vez
            init_x=event.getX();
            return true;
        case MotionEvent.ACTION_UP: //Cuando el usuario deja de presionar
            float distance =init_x-event.getX();
            if(distance>0)
            {
                 vf.setInAnimation(inFromRightAnimation());
                 vf.setOutAnimation(outToLeftAnimation());
                 vf.showPrevious();
            }
            if(distance<0)
            {
                 vf.setInAnimation(inFromLeftAnimation());
                 vf.setOutAnimation(outToRightAnimation());
                 vf.showNext();
            }
        default:
            break;
        }
        return false;
    }

private Animation inFromRightAnimation() {
    Animation inFromRight = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
    Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f );
    inFromRight.setDuration(500);
    inFromRight.setInterpolator(new AccelerateInterpolator());
    return inFromRight;
}
private Animation outToLeftAnimation() {
    Animation outtoLeft = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, -1.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f);
    outtoLeft.setDuration(500);
    outtoLeft.setInterpolator(new AccelerateInterpolator());
    return outtoLeft;
}
private Animation inFromLeftAnimation() {
    Animation inFromLeft = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, -1.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f);
    inFromLeft.setDuration(500);
    inFromLeft.setInterpolator(new AccelerateInterpolator());
    return inFromLeft;
}
private Animation outToRightAnimation() {
    Animation outtoRight = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, +1.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f);
    outtoRight.setDuration(500);
    outtoRight.setInterpolator(new AccelerateInterpolator());
    return outtoRight;
}

}
